package cn.hfatec.shms.web;

import cn.hfatec.JsonResponse;
import cn.hfatec.constants.SysContant;
import cn.hfatec.shms.base.ShmsBaseAction;
import cn.hfatec.shms.constants.ShmsConstant;
import cn.hfatec.shms.exception.NotAuthException;
import cn.hfatec.shms.system.roleHome.model.RoleHomeView;
import cn.hfatec.shms.system.roleHome.service.RoleHomeService;
import cn.hfatec.shms.system.user.model.User;
import cn.hfatec.shms.system.userRole.service.UserRoleService;
import cn.hfatec.shms.web.amb.ambHymxb.service.AmbHymxbService;
import cn.hfatec.shms.web.qz.qzByl.service.QzBylService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: panwb
 * <p/>
 * Date: 2015/11/7
 * Time: 15:58
 */
@Controller
@RequestMapping("/")
public class HomeAction extends ShmsBaseAction {
    //服务层
    @Resource(name = "AmbHymxbService")
    private AmbHymxbService ambHymxbService;
    @Resource(name = "UserRoleService")
    private UserRoleService userRoleService;
    @Resource(name = "RoleHomeService")
    private RoleHomeService roleHomeService;
    //服务层
    @Resource(name = "QzBylService")
    private QzBylService qzBylService;

    @RequestMapping
    public String homePage() {
        return "redirect:/home";
    }

    @RequestMapping("/home")
    public String homePage2(Model model, HttpServletRequest request) throws NotAuthException {
        User user = (User) request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
        if (user == null) {
            throw new NotAuthException("用户未授权");
        }


        List list = userRoleService.queryUserRoleByUserId(user.getLoginName());
        List<RoleHomeView> roleHomeViewList = roleHomeService.queryHomeListByRoleIds(list, ShmsConstant.SHORTCUTS_FUN);
        //面板功能配置
        List<RoleHomeView> panelFuncList = roleHomeService.queryHomeListByRoleIds(list, ShmsConstant.PANEL_FUN);

        model.addAttribute("roleHomeViewList", roleHomeViewList);
        model.addAttribute("panelFuncList", panelFuncList);
        //待办事项总数
        int a=10;
        model.addAttribute("totalBacklog",a);


        return "home";
    }

    /**
     * 过期会员，有效会员图表
     *
     * @param request
     * @return
     * @throws NotAuthException
     */
    @RequestMapping("/ajaxCustomerState")
    @ResponseBody
    public String ajaxCustomerState(HttpServletRequest request) throws NotAuthException {
        User user = (User) request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
        if (user == null) {
            throw new NotAuthException("用户未授权");
        }
        int[] a = new int[5];
        a[0] = ambHymxbService.ambOverdueCustomer(0);
        a[1] = ambHymxbService.ambOverdueCustomer(1);
        a[2] = ambHymxbService.ambOverdueCustomer(2);
        a[3] = ambHymxbService.ambOverdueCustomer(3);
        a[4] = ambHymxbService.ambOverdueCustomer(4);
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", a).toString();
    }


    /**
     * 过期会员，有效会员图表
     *
     * @param request
     * @return
     * @throws NotAuthException
     */
    @RequestMapping("/ajaxCustomerFgState")
    @ResponseBody
    public String ajaxCustomerFgState(HttpServletRequest request) throws NotAuthException {
        User user = (User) request.getSession().getAttribute(SysContant.LOGIN_USER_SESSION);
        if (user == null) {
            throw new NotAuthException("用户未授权");
        }
        int[] a = new int[5];
        a[0] = ambHymxbService.amboverdueFgCustomer(user.getUserName(), 0);
        a[1] = ambHymxbService.amboverdueFgCustomer(user.getUserName(), 1);
        a[2] = ambHymxbService.amboverdueFgCustomer(user.getUserName(), 2);
        a[3] = ambHymxbService.amboverdueFgCustomer(user.getUserName(), 3);
        a[4] = ambHymxbService.amboverdueFgCustomer(user.getUserName(), 4);
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", a).toString();
    }
    /**
     * 保有量
     *
     * @param request
     * @return
     * @throws NotAuthException
     */
    @RequestMapping("/ajaxQzBylState")
    @ResponseBody
    public String ajaxQzBylState(HttpServletRequest request) throws NotAuthException {
         List list=qzBylService.qzLine();
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", list).toString();
    }
}
